//
//  PCDPageViewMange.m
//  PCDBank
//
//  Created by DK on 2021/2/25.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDPageViewMange.h"
#import "PCDBankWebViewController.h"
#import "PCDDataBaseModel.h"

#define EMPTYPAGE  @"#/ares/empty/empty"

#define DefoutAPPID  @"df123456"

@interface PCDPageViewMange()

@property(nonatomic,retain) NSMutableArray *viewArray;

@end

@implementation PCDPageViewMange

-(instancetype) init
{
    if (self =[super init]) {
        self.viewArray = [[NSMutableArray array] init];
        self.baseUrlList = [[NSMutableArray array] init];
    }
    return self;
}

-(UIViewController *) creatCacheWebController:(NSDictionary *)params View:(UIViewController*)view Status:(BOOL)status
{
    [self initData:params View:view];

    PCDBankWebViewController *viewController= (PCDBankWebViewController *)[self createWebView:params Status:status];
    viewController.appId = self.appId;
    viewController.index = self.index;

    return viewController;
}

-(void) creatCacheWebControllerWithView:(UIViewController*)view Params:(NSDictionary *)params Status:(BOOL)status
{
    [self initData:params View:view];

    PCDBankWebViewController *viewController = (PCDBankWebViewController *)[self createWebView:params Status:status];
    viewController.appId = self.appId;
    viewController.index = self.index;

    if (view) {
        [view.navigationController pushViewController:viewController animated:YES];
    }else if(self.baseNavgatView)
    {
        [self.baseNavgatView pushViewController:viewController animated:YES];
    }
}

-(void)initData:(NSDictionary *)params View:(UIViewController*)view
{
    NSString *strAppid = [params objectForKey:@"appId"];
    NSString *strBaseUrl = [params objectForKey:@"baseurl"];
    
    //更新baseUrl
    if ([PCDUtil nilOrEmptyString:strBaseUrl]) {
        strBaseUrl = params[@"baseUrl"];
    }
    
    NSString *extraData = [params objectForKey:@"extraData"];
    [PCDDataBaseModel shareModel].extraData = extraData;
    
    if ([PCDUtil nilOrEmptyString:strAppid] && [PCDUtil nilOrEmptyString:self.appId]) {
        strAppid = DefoutAPPID;
        
    }else if ([PCDUtil nilOrEmptyString:strAppid] && ![PCDUtil nilOrEmptyString:self.appId])
    {
        strAppid = self.appId;
    }
    
    if (![self.appId isEqual:strAppid]) {
        self.appId = strAppid;
        self.index = 1;
    }else{
        self.index ++;
    }
    
    if (![PCDUtil nilOrEmptyString:strBaseUrl]) {
        self.baseUrl = strBaseUrl;
    }
    
    if (self.baseUrl) {
        [self.baseUrlList addObject:self.baseUrl];
    }


    if (view) {
        self.baseNavgatView = view.navigationController;
        [self.viewArray removeAllObjects];
    }

}

-(UIViewController *)createWebView:(NSDictionary *)params  Status:(BOOL)status
{
    NSString *strCUrl = [params objectForKey:@"curl"];
    strCUrl = [PCDUtil stringByTrimming:strCUrl];
    NSString *strUrl = [params objectForKey:@"url"];
    strUrl = [PCDUtil stringByTrimming:strUrl];
    if ([PCDUtil nilOrEmptyString:strUrl] && ![PCDUtil nilOrEmptyString:strCUrl]) {
        if ([self.baseUrl containsString:self.appId]) {
            strUrl = [NSString stringWithFormat:@"%@#%@",self.baseUrl,strCUrl];
        }else {
            strUrl = [NSString stringWithFormat:@"%@#%@/%@",self.baseUrl, self.appId,strCUrl];
        }
    }
    
    NSDictionary *pDic = @{@"url":strUrl};
    UIViewController *webPageView;
    
    if (status) {
        
        NSString *strEmptyUrl = [NSString stringWithFormat:@"%@%@",self.baseUrl,EMPTYPAGE];
        
        NSDictionary *empDic = @{@"url":strEmptyUrl};
        
        PCDBankWebViewController *viewController= (PCDBankWebViewController *)[[CTMediator sharedInstance] CTMediatorH5ContainerViewController:empDic];
        
        if (self.viewArray.count) {
            PCDBankWebViewController *tempPage = (PCDBankWebViewController *)[self.viewArray objectAtIndex:0];
            [tempPage webViewRefresh:strCUrl];
            webPageView = tempPage;
        }else{
            webPageView =(PCDBankWebViewController *)[[CTMediator sharedInstance] CTMediatorH5ContainerViewController:pDic];
        }
        
        if (self.viewArray.count) {
            [self.viewArray removeAllObjects];
        }
        
        [self.viewArray addObject:viewController];
        
    }else{
        webPageView =  (PCDBankWebViewController *)[[CTMediator sharedInstance] CTMediatorH5ContainerViewController:pDic];
    }
    
    return webPageView;
}

@end

#ifdef __cplusplus
extern "C" {
#endif
    PCDPageViewMange * PCDPageViewMangeGet(){
        
        static PCDPageViewMange *mangeGet = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            mangeGet = [[PCDPageViewMange alloc] init];

        });

        return mangeGet;
    }
#ifdef __cplusplus
}
#endif
